/*
 * 队列 (Queue) - C#实现
 * C#提供内置Queue<T>，FIFO，所有操作O(1)
 */

using System;
using System.Collections.Generic;

namespace DataStructures
{
    public class MyQueue<T>
    {
        private LinkedList<T> _items = new LinkedList<T>();

        public void Enqueue(T item) => _items.AddLast(item);
        
        public T Dequeue()
        {
            if (IsEmpty) throw new InvalidOperationException("队列为空");
            T item = _items.First.Value;
            _items.RemoveFirst();
            return item;
        }
        
        public T Peek()
        {
            if (IsEmpty) throw new InvalidOperationException("队列为空");
            return _items.First.Value;
        }
        
        public bool IsEmpty => _items.Count == 0;
        public int Count => _items.Count;
    }

    public class QueueExamples
    {
        // 打印队列
        public static void PrintQueue()
        {
            Console.WriteLine("\n=== 打印任务队列 ===");
            var queue = new Queue<string>();
            
            string[] tasks = { "report.pdf", "invoice.pdf", "manual.pdf" };
            foreach (var task in tasks)
            {
                queue.Enqueue(task);
                Console.WriteLine($"  添加: {task}");
            }
            
            Console.WriteLine("\n开始打印:");
            while (queue.Count > 0)
                Console.WriteLine($"  正在打印: {queue.Dequeue()}");
            
            Console.WriteLine("优势: FIFO保证公平性");
        }
    }

    public class QueueProgram
    {
        public static void Main()
        {
            Console.WriteLine("╔════════════════════════════════════════╗");
            Console.WriteLine("║   队列 (Queue) - C#实现                ║");
            Console.WriteLine("╚════════════════════════════════════════╝");

            var queue = new MyQueue<int>();
            queue.Enqueue(10);
            queue.Enqueue(20);
            queue.Enqueue(30);
            Console.WriteLine($"\n出队: {queue.Dequeue()}");
            Console.WriteLine($"队头: {queue.Peek()}");
            Console.WriteLine($"大小: {queue.Count}");

            QueueExamples.PrintQueue();

            Console.WriteLine("\n✅ 优势: 所有操作O(1)，FIFO保证公平");
            Console.WriteLine("⚠️ 局限: 只能访问队头和队尾");
            Console.WriteLine("💡 应用: 任务调度、BFS、消息队列");
        }
    }
}
